package com.beiding.mpTimer;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 该类是时间轴服务的实现
 * 将时间轴服务所需的数据拆分成四个独立的接口
 */
public class TimerShaftServiceImpl implements TimerShaftService {


    private TSDescription description;

    private TSPointSortedSet sortedSet;

    private TSPointIdLists lists;

    private TSTaskMap map;


    public TimerShaftServiceImpl(TSDescription description, TSPointSortedSet sortedSet, TSPointIdLists lists, TSTaskMap map) {
        this.description = description;
        this.sortedSet = sortedSet;
        this.lists = lists;
        this.map = map;
    }

    @Override
    public String getTimerShaftHolderId() {
        return description.getTimerShaftHolderId();
    }

    @Override
    public void setTimerShaftHolderId(String id) {

        description.setTimerShaftHolderId(id);

    }

    @Override
    public void updateLastHeartbeatTimestamp(long timeMillis) {

        description.updateLastHeartbeatTime(timeMillis);

    }

    @Override
    public Map<Long, Iterable<String>> getOverdueTasks(long time) {

        //首先从点集合中找到所有的过期时间点

        Iterable<Long> iterable = sortedSet.getPointsLtTime(time);

        if (iterable != null) {

            Map<Long, Iterable<String>> ots = new HashMap<>();

            iterable.forEach(aLong -> {

                List<String> list = lists.get(aLong);

                //如果节点为0
                if (list.size() == 0) {
                    lists.remove(aLong);
                    sortedSet.remove(aLong);
                } else {
                    ots.put(aLong, list);
                }

            });

            if (ots.size() == 0) {
                return null;
            }

            return ots;

        }

        return null;
    }

    @Override
    public Iterable<String> getOnTimeTasks(long time) {

        List<String> list = lists.get(time);

        if (list == null) {
            return Collections.emptyList();
        }

        if (list.size() == 0) {

            lists.remove(time);
            sortedSet.remove(time);

            return Collections.emptyList();
        }

        return list;
    }

    @Override
    public long getLastHeartbeatTimestamp() {
        return description.getLastHeartbeatTime();
    }

    @Override
    public long getStep() {
        return description.getStep();
    }

    @Override
    public long getHeartbeatInterval() {
        return description.getHeartbeatInterval();
    }

    @Override
    public void setHeartbeatInterval(long heartbeatInterval) {

        description.setHeartbeatInterval(heartbeatInterval);
    }

    @Override
    public void setStep(long step) {

        description.setStep(step);

    }

    @Override
    public void permanent(TaskPersistence persistence) {
        map.permanent(persistence);

        lists.addTask(persistence.getTime(), persistence.getId());

        sortedSet.flag(persistence.getTime());

    }

    @Override
    public Object remove(String tackId) {
        return map.remove(tackId);
    }

    @Override
    public TaskPersistence getTask(String id) {
        return map.get(id);
    }

}
